<template>
  <div>
    <el-dialog
      :title="type === 1 ? '新增行业' : '编辑行业'"
      :visible="showDialog"
      width="400px"
      @close="btnCancel"
    >
      <el-form
        ref="indusContent"
        :model="indusForm"
        :rules="rules"
        label-width="80px"
        style="padding-top:10px"
      >
        <el-form-item label="行业名称" prop="industryname">
          <el-input v-model="indusForm.industryname" style="width:200px"/>
        </el-form-item>
        <el-form-item label="排序" prop="number">
          <el-input v-model.number="indusForm.number" :disabled="flag" style="width:100px"/>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex">
        <el-col>
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// import { setData } from '@/utils/localstorage'
import { addIndustry, editIndustry } from '@/api/trade'
export default {
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      indusForm: {
        industryname: '',
        number: ''
      },
      rules: {
        industryname: [
          { required: true, message: '请输入行业名称', trigger: 'blur' },
          {
            max: 10,
            message: '长度最大为 10 个字符',
            trigger: 'blur'
          }
        ],
        number: [
          { required: true, message: '请输入行业排序', trigger: 'blur' },
          {
            type: 'number',
            message: '必须为数字且最大为5位数',
            trigger: 'blur'
          }
        ]
      },
      flag: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    btnCancel() {
      this.indusForm = {
        industryname: '',
        number: ''
      }
      this.$refs.indusContent.resetFields()
      this.$emit('update:showDialog', false)
    },
    async btnOK() {
      // 编辑
      if (this.indusForm.id) {
        // console.log('编辑')

        await editIndustry(this.indusForm)
        this.$message.success('编辑成功')
      } else {
        // 新增
        await this.$refs.indusContent.validate()
        try {
          await addIndustry(this.indusForm)
          // this.$message.success('添加行业成功!')
        } catch (error) {
          console.log(error)
        }
      }
      this.$parent.getIndustry()
      // this.$emit('addIndus')
      // setData();
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
<style scoped></style>
